<template>
  <div class="layout">
    <el-container>
      <el-header class="header">
          <div class="logo">志愿服务管理中心<a class="a-link " @click="goToHome" style="margin-left: 20px; font-size: 14px;cursor: pointer; background-color: rgb(237, 242, 250);padding: 4px 8px 4px 8px;border-radius: 10px;">活动</a></div>
        <div class="userInfo">
          <span>欢迎回来,</span>
          <el-dropdown trigger="click">
            <span class="nickName">
              {{ userInfo.name }}
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>
                  <router-link class="a-link" to="/setting/myInfo"
                    >个人信息</router-link
                  >
                </el-dropdown-item>
                <el-dropdown-item
                  ><a href="javascript:void(0)" class="a-link" @click="logout"
                    >退出登录</a
                  ></el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>

          <div class="avatar"><img :src="`/api/file/getAvatar/${userInfo.userId}`"/></div>
        </div>

      </el-header>
      <el-container class="container">
        <el-aside width="200px" class="left-aside">
          <ul class="menu-panel">
            <li v-for="(menu, index) in menuList" :key="index" v-show="menu.roleType!=null&& userInfo.roleType==menu.roleType||userInfo.roleType==1||userInfo.roleType==2">
              <span class="menu-title-p" @click="openClose(index)" >
                <span :class="['iconfont', menu.icon]"></span>
                <span class="menu-title">{{ menu.title }}</span>
                <span
                  :class="[
                    'iconfont',
                    'open-closes',
                    menu.open ? 'icon-open-copy' : 'icon-closes',
                  ]"
                  style="font-size: 14px"
                ></span>
              </span>
              <ul class="sub-menu" v-show="menu.open">
                <li
                  v-for="(subMenu, subIndex) in menu.children"
                  :key="subIndex"
                >
                  <router-link
                    :class="[
                      'sub-menu-item',
                      activePath == subMenu.path ? 'active' : '',
                    ]"
                    :to="subMenu.path"
                    v-if="subMenu.roleType==null||(subMenu.roleType!=null&&subMenu.roleType==userInfo.roleType)"
                  >
                    {{ subMenu.title }}</router-link
                  >
                </li>
              </ul>
            </li>
          </ul>
        </el-aside>
        <el-main class="right-main"><router-view></router-view></el-main>
      </el-container>
    </el-container>
  </div>

</template>
<script setup>
import { ref, reactive, getCurrentInstance, nextTick, watch } from "vue";
const { proxy } = getCurrentInstance();

import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();


const api = {
  getUserInfo: "/getUserInfo",
  logout: "/account/logout",
};

const userInfo=ref(proxy.VueCookies.get("userInfo"));


const activePath = ref(null);
watch(
  route,
  (newVal, oldVal) => {
    activePath.value = newVal.path;
  },
  { immediate: true, deep: true }
);

//菜单列表
const menuList = ref([
{
    title: "首页",
    icon: "icon-home",
    open: true,
    roleType:0,
    children: [
      {
        title: "首页",
        path: "/home",
      },
      {
        title: "活动",
        path: "/site/project",
      }
    ],
  },
  {
    title: "系统管理",
    icon: "icon-systting",
    open: true,
    roleType:1,
    children: [
      {
        title: "用户管理",
        path: "/setting/user",
      },
      {
        title: "活动管理",
        path: "/activity/activitylist",
      },
      {
        title: "申请管理",
        path: "/apply/applyList",
      },
      {
        title: "评分管理",
        path: "/rating/ratinglist",
      },
    ],
  },
  {
    title: "个人信息",
    icon: "icon-users",
    open: true,
    roleType:0,
    children: [
      {
        title: "我的信息",
        path: "/setting/myInfo",
      },
      {
        title: "申请记录",
        path: "/user/applyInfo",
      },
      {
        title: "志愿记录",
        path: "/user/recordInfo",
      },
    ],
  },
]);

//展开关闭
const openClose = (index) => {
  let open = menuList.value[index].open;
  menuList.value[index].open = !open;
};

//退出
const logout = () => {
  proxy.Confirm(`你确定要退出吗?`, async () => {
    let result = await proxy.Request({
      url: api.logout,
    });
    if (!result) {
      return;
    }
    proxy.Message.success("退出成功");
    debugger;
    router.push("/login");
  });
};


const goToHome=()=>{
  router.push({ path: '/site/project' });
}




</script>

<style lang="scss" scoped>
.layout {
  .header {

    border-bottom: 1px solid #ddd;
    display: flex;
    align-items: center;
    height: 60px;
    justify-content: space-between;
    .logo {
      margin-left: 30px;
      font-size: 20px;
      color: rgb(29, 32, 136);
      font-weight: bolder;
    }
    .userInfo {
      display: flex;
      align-items: center;
      .nickName {
        font-size: 20px;
        color: royalblue;
        cursor: pointer;
      }
      .avatar {
        width: 40px;
        margin-left: 10px;
        img {
          border-radius: 50%;
          width: 40px;
        }
      }
    }
  }
  .container {
  padding-top: 10px;
  background: #f5f6f7;
  height: calc(100vh - 60px);
}

.left-aside {
  padding: 0px 15px;
  width: 280px;
  background: #fff; /* 背景颜色 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); /* 添加阴影 */
  border-radius: 8px; /* 圆角 */
  margin-right: 20px; /* 与右侧内容区分开 */
}

.post-btn {
  background: linear-gradient(90deg, #fc5531, #ff8b50); /* 渐变色背景 */
  color: #fff;
  height: 40px;
  width: 88%;
  margin-left: 12%;
  font-size: 18px;
  font-weight: bold;
  border-radius: 60px;
  transition: all 0.3s ease; /* 平滑过渡 */
  box-shadow: 0 4px 10px rgba(252, 85, 49, 0.4); /* 按钮阴影 */
}

.post-btn:hover {
  transform: scale(1.05); /* 悬停时放大 */
  box-shadow: 0 6px 15px rgba(252, 85, 49, 0.5); /* 加大阴影 */
}

.menu-panel {
  margin-top: 20px;
 ul,
  li {
    padding: 0px;
    margin: 0px;
    list-style: none; /* 这行代码已经去掉了ul和li的项目符号 */
  }
  .menu-title-p {
    line-height: 45px;
    cursor: pointer;
    display: flex;
    align-items: center; /* 垂直居中 */
    padding: 0 12px;
    transition: background-color 0.3s ease, color 0.3s ease;
    border-radius: 8px; /* 圆角 */
  }

  .menu-title-p:hover {
    background: #f0f0f0;
    color: #2c3e50;
  }

  .iconfont {
    font-size: 18px;
    color: #91949a;
    margin-right: 15px; /* 图标与文本之间留有空间 */
  }

  .menu-title {
    flex: 1;
    color: #3f4042;
    font-weight: 500; /* 加粗 */
  }

  .sub-menu {
    font-size: 14px;
    margin-left: 15px; /* 子菜单缩进 */

    .sub-menu-item {
      line-height: 40px;
      padding: 0 10px 0 40px;
      display: block;
      cursor: pointer;
      text-decoration: none;
      color: #3f4042;
      transition: background-color 0.3s ease, color 0.3s ease, transform 0.3s ease;
      border-radius: 50px;
    }

    .active,
    .sub-menu-item:hover {
      background: #f5f1f1;
      color: #1d2088;
      border-radius: 50px;
      font-weight: bold;
      transform: translateX(10px); /* 悬停时向右移动 */
    }
  }
}

.right-main {
  position: relative;
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); /* 添加阴影 */
}
}
</style>
